// pages/aiPaper/aiPaper.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    initialState: false, // 页面元素初始显示状态
    problemArray: [],
    key: null, //后台缓存标识
    testState: true, //题目初始状态
    submitstatus: false // 提交状态
  },

  /**
   * 在线答题入口
   */
  onlineAnswer: function() {
    var _this = this,
      url = _this.data.url,
      key = _this.data.key;
    wx.navigateTo({
      url: '/pages/zyWebView/zyWebView?url=' + url + '/kmapping/Webapp/kmappingPaperDetail&key=' + key + '&subscript=0&index=0',
    })
  },

  /**
   * 自动生成智能测验卷
   */
  getAnswerInfo: function() {
    var _this = this,
      structure_id = _this.data.structure_id,
      stage_id = _this.data.stage_id,
      subject_id = _this.data.subject_id;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.URL + '/kmapping/WebAppAjax/autoCreateDiagnosisPaper?' + app.globalData.URLBOOL,
      method: 'GET',
      data: {
        structure_id: structure_id,
        stage_id: stage_id,
        subject_id: subject_id
      },
      success(res) {
        wx.hideLoading();
        if (res.data.error_code == 0) {
          var _problemArray = [res.data.data];
          _this.setData({
            problemArray: _problemArray,
          })
        } else if (res.data.error_code == 10002) {
          _this.setData({
            problemArray: []
          })
        }
        _this.fromCache();
        _this.setData({
          initialState: true
        })
      }
    })

  },

  /**
   * 智能测验卷的错对显示
   */
  getDiagonsisPaperInfo: function(problemArray) {
    var _this = this;
    var _problemArray = [problemArray];
    _this.setData({
      problemArray: _problemArray,
      submitstatus: true
    })
    _this.fromCache();
  },

  /**
   * 将题目信息录入缓存
   */
  fromCache: function(cacheName) {
    var _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.URL + '/kmapping/WeixinAppAjax/addTopicAnswerToCache?' + app.globalData.URLBOOL,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        topic_info: JSON.stringify(_this.data.problemArray)
      },
      success: function(res) {
        var _res = res.data;
        wx.hideLoading();
        if (_res.error_code == 0) {
          var key = _res.data.key;
          _this.setData({
            key: key
          })
        }
      }
    })
  },

  /**
   * 提交答案
   */
  submitAnswer: function() {
    var _this = this,
      problemArray = _this.data.problemArray,
      user_id = wx.getStorageSync('userId'),
      paper_id = problemArray[0].id;
    var _answer = [];
    var topics = problemArray[0].topics;
    for (var k = 0; k < topics.length; k++) {
      var myAnswer = {
        "resource_type": 0,
        "answer": "",
        "resource_mds": "",
        "topic_id": null
      };
      if (typeof topics[k].paper_answer != 'undefined') {
        myAnswer.answer = topics[k].paper_answer.answer.stu_answer;
      }
      myAnswer.topic_id = topics[k].id;
      _answer.push(myAnswer);
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交?',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '答案上传中',
          })
          wx.request({
            url: app.globalData.URL + '/kmapping/WebAppAjax/submitDiagnosisAnswer?' + app.globalData.URLBOOL,
            method: 'GET',
            data: {
              paper_id: paper_id,
              answer: JSON.stringify(_answer),
              user_id: user_id
            },
            success(res) {
              wx.hideLoading();
              if (res.data.error_code == 0) {
                wx.showToast({
                  title: '习题答案提交成功',
                  icon: 'none',
                  duration: 2000
                })
                _this.getDiagonsisPaperInfo(res.data.data);
              } else {
                wx.showToast({
                  title: res.data.error_msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    var _this = this,
      structure_id = options.structure_id,
      subject_id = options.subject_id,
      stage_id = options.stage_id;
    _this.setData({
      url: app.globalData.URL,
      structure_id: options.structure_id,
      subject_id: options.subject_id,
      stage_id: options.stage_id
    })
    _this.getAnswerInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var _this = this,
      problemArray = _this.data.problemArray,
      onlineState = _this.data.onlineState;
    // var paperIdArr = paper_ids.split(",");
    if (onlineState) { //在线答题发回答题卡
      var topics = problemArray[0].topics;
      for (var i = 0; i < topics.length; i++) {
        if (topics[i].answer_flag == 1) {
          _this.setData({
            testState: false
          })
          break;
        }
      }
      _this.fromCache();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})